<?php

namespace App\Models;

use App\Models\Rbac\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserLog extends BaseModel
{
    use HasFactory;
    protected $table = 'user_log';
    protected $fillable = ['user_id','path','method','ip','input','run_time','response',
        'res','created_at','updated_at','route_name'];

    public function scopeSearch($query,$params)
    {
        if(!empty($params['path'])){
            $query->where('path',$params['path']);
        }
        if(!empty($params['user_id'])){
            $query->where('user_id',$params['user_id']);
        }
        if(!empty($params['ip'])){
            $query->where('ip',$params['ip']);
        }
        if(!empty($params['res'])){
            $query->where('res',$params['res']);
        }
        return $query;
    }

    public function User(){
        return $this->hasOne(User::class,'id','user_id')->select('username','company_name','id');
    }
}
